
const db = require('../db/index')


// admin添加用户
exports.useradminadd = (req, res) => {
  let data = req.body
  data.appid = JSON.stringify(data.appid)
  if (data._id) {
    let id = data._id
    delete data._id
    const sql = `update adminusers set ? where _id = ${id}`
    db.query(sql, [data], (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        message: '修改成功'
      })
    })
  } else {
    const selectSql = `select * from adminusers where phone = ?`
    db.query(selectSql, data.phone, (err, res2) => {
      if (err) return res.cc(err)
      if (res2.length > 0) {
        res.cc({ res: '账号重复' })
      } else {
        data.identiy = 1
        const sql = `insert into adminusers set ?`
        db.query(sql, data, (err, res2) => {
          if (err) return res.cc(err)
          res.send({
            res: '添加成功'
          })
        })
      }
    })

  }

}
// 获取用户列表
exports.userList = (req, res) => {
  let pageData = req.body.pageData
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  const sql = `select * from adminusers limit ${size} OFFSET  ${from}`
  db.query(sql, (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from adminusers`
    db.query(sql2, (err2, res3) => {
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 删除用户
exports.userdelete = (req, res) => {
  let id = req.query._id
  const sql = `delete from adminusers where _id in(${id})`
  db.query(sql, (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}
// 添加小程序
exports.appAdd = (req, res) => {
  let data = req.body
  delete data.appidPassword
  const sql = `insert into apps set ?`
  let sql2 = `insert into menus set ?`
  let menus = [
    {
      ruterName: '资源管理',
      pId: data.appid,
      ruterData: 'resource',
      appid: ''
    }, {
      ruterName: '编号管理',
      pId: data.appid,
      ruterData: 'number',
      appid: ''
    }, {
      ruterName: '门店管理',
      pId: data.appid,
      ruterData: 'store',
      appid: ''
    }, {
      ruterName: '车主管理',
      pId: data.appid,
      ruterData: 'carOwner',
      appid: ''
    }, {
      ruterName: '报价管理',
      pId: data.appid,
      ruterData: 'quotationManagement',
      appid: ''
    }, {
      ruterName: '质保管理',
      pId: data.appid,
      ruterData: 'qualit',
      appid: ''
    }, {
      ruterName: '车险管理',
      pId: data.appid,
      ruterData: 'insurance',
      appid: ''
    }, {
      ruterName: '周边管理',
      pId: data.appid,
      ruterData: 'shopping',
      appid: ''
    }
  ]
  let icons = [
    {
      name: '产品介绍',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '案例欣赏',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '质保',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 1
    }, {
      name: '我的',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '我的收藏',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '我的订单',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '我的质保',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '车辆管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '车衣保险',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '我的预约',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '关于我们',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 2
    }, {
      name: '质保管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '库存管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '预约管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '订单管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '发货管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '收货管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '门店管理',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '门店审核',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '关于我们',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 3
    }, {
      name: '首页',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 1
    }, {
      name: '门店',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 1
    }, {
      name: '周边',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 1
    }, {
      name: '质保查询',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '关于我们',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '漆面膜',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '改色膜',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '车顶膜',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    }, {
      name: '太阳膜',
      selectImg: '',
      unSelectImg: '',
      backImg: '',
      appid: '',
      remark: '',
      type: 0
    },
  ]

  let imageText = [{
    name: '首页底图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '质保查询页顶部头图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '质保详情页顶部头图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '质保卡底图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '我的页顶部头图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '登录页底图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '车衣险流程图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '车衣险背景图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '报价背景图',
    image: '',
    text: '',
    remark: '',
    type: '0'
  }, {
    name: '车衣险案例分析图',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '质保范围',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '漆面保护膜',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: 'TPU改色膜',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '太阳膜',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '镀金隔热天幕',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '保养方法',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '免责条款',
    image: '',
    text: '',
    remark: '',
    type: '1'
  }, {
    name: '车衣险',
    image: '',
    text: '',
    remark: '',
    type: '2'
  }, {
    name: '除外责任',
    image: '',
    text: '',
    remark: '',
    type: '2'
  }, {
    name: '用户协议及隐私条款',
    image: '',
    text: '',
    remark: '',
    type: '2'
  }, {
    name: '除外责任',
    image: '',
    text: '',
    remark: '',
    type: '2'
  },]
  let iconsAdd = `insert into icons set ?`
  let imgortextAdd = `insert into imgortext set ?`
  db.query(sql, [data], (err, res2) => {
    if (err) return res.cc(err)
    menus.forEach((item) => {
      item.appid = res2.insertId
      db.query(sql2, [item], (err, res3) => {
        if (err) return res.cc(err)

      })

    })
    icons.forEach((item) => {
      item.appid = res2.insertId
      db.query(iconsAdd, [item], (err, res4) => {
        if (err) return res.cc(err)
      })
    })
    imageText.forEach((item) => {
      item.appid = res2.insertId
      db.query(imgortextAdd, [item], (err, res4) => {
        if (err) return res.cc(err)
      })
    })
    let adminSql = `select * from adminusers where identiy=0`
    let insertAdmin = `update adminusers set appid=? where _id= ?`
    db.query(adminSql, (err, res3) => {
      if (err) return res.cc(err)
      res3.forEach((item) => {
        item.appid = item.appid.split(',')
        item.appid.push(res2.insertId)
        item.appid = item.appid.join(',')
        db.query(insertAdmin, [item.appid, item._id], (err, res4) => {
          if (err) return res.cc(err)
          res.send({
            message: '添加成功',
            type: 1
          })
        })
      })
    })

  })

}